<template>
  <div>
    <breadcrumbs :data1="data1" :data2="data2"></breadcrumbs>
    <el-card>
      <el-row>
        <el-button type="primary" @click="addClassBtn">添加分类</el-button>
      </el-row>
      <tree-table
        class="table"
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        border
        :show-row-hover="false"
      >
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted === false"
            style="color:lightgreen"
          ></i>
          <i class="el-icon-error" v-else style="color:lightgreen"></i>
        </template>
        <template slot="class" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level == 0" type="success" size="mini">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level == 1" type="primary" size="mini">二级</el-tag>
          <el-tag v-else-if="scope.row.cat_level == 2" type="warning" size="mini">三级</el-tag>
        </template>
        <template slot="handle" slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="editCateBtn(scope.row.cat_id)"
            icon="el-icon-edit"
          >编辑</el-button>
          <el-button
            type="danger"
            size="mini"
            @click="delCateBtn(scope.row.cat_id)"
            icon="el-icon-delete"
          >删除</el-button>
        </template>
      </tree-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="this.queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
      ></el-pagination>
      <!-- 弹出添加分类对话框 -->
      <el-dialog title="添加分类" :visible.sync="addClassdialogVisible" width="50%" @close="closeForm">
        <el-form
          :model="addClassInfo"
          :rules="addClassrules"
          ref="addClassForm"
          label-width="110px"
        >
          <el-form-item label="分类名称：" prop="cat_name">
            <el-input v-model="addClassInfo.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类：">
            <el-cascader
              v-model="selectedKeys"
              :options="ParentCateList"
              :props="{ expandTrigger: 'hover',value:'cat_id',label:'cat_name',children:'children',checkStrictly: true}"
              @change="parentCateChange"
              clearable
            ></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addClassdialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addClass">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 弹出编辑分类的对话框 -->
      <el-dialog title="编辑分类" :visible.sync="editClassdialogVisible" width="50%">
        <el-form
          :model="editClassInfo"
          :rules="addClassrules"
          ref="editClassForm"
          label-width="110px"
        >
          <el-form-item label="分类名称：" prop="cat_name">
            <el-input v-model="editClassInfo.cat_name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer">
          <el-button @click="editClassdialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editCate">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
           
<script>
import breadcrumbs from '@/common/breadcrumbs'
export default {
  data () {
    return {
      editClassdialogVisible: false,
      addClassdialogVisible: false,
      data1: '商品管理',
      data2: '商品分类',
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      cateList: [],
      ParentCateList: [],
      columns: [
        { label: '商品分类', prop: 'cat_name' },
        { label: '是否可用', template: 'isok', type: 'template' },
        {label: '分类', template: 'class', type: 'template'},
        { label: '操作', type: 'template', template: 'handle' }
      ],
      addClassInfo: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0
      },
      addClassrules: {
        cat_name: [
          { required: true, message: '分类名称不能为空！', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
      },
      selectedKeys: [],
      editId: 0,
      editClassInfo: {
        cat_name: ''
      }
    }
  },
  components: {
    breadcrumbs
  },
  methods: {
    async getGoodsList () {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryInfo
      })
      this.cateList = res.data.result
      this.total = res.data.total
    },
    async editCateBtn (id) {
      this.editClassdialogVisible = true
      this.editId = id
      const { data: res } = await this.$http.get(`categories/${id}`)
      this.editClassInfo.cat_name = res.data.cat_name
    },
    async editCate () {
      this.$refs.editClassForm.validate(async val => {
        if (val) {
          const { data: res } = await this.$http.put(`categories/${this.editId}`, this.editClassInfo)
          if (res.meta.status == 200) {
            this.$message.success(`修改分类成功~`)
            this.getGoodsList()
            this.editClassdialogVisible = false
          } else {
            this.$message.error(res.meta.msg)
          }
        }
      })
    },
    async delCateBtn (id) {
      const { data: res } = await this.$http.delete(`categories/${id}`)
      if (res.meta.status == 200) {
        this.$message.success('删除分类成功~')
        this.getGoodsList()
      } else {
        this.$message.error(res.meta.msg)
      }
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    addClassBtn () {
      this.getParentCateList()
      this.addClassdialogVisible = true
    },
    async getParentCateList () {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      this.ParentCateList = res.data
    },
    async parentCateChange () {
      // console.log(this.selectedKeys); 
      if (this.selectedKeys.length > 0) {
        this.addClassInfo.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
      }
      if (this.selectedKeys.length > 0) {
        this.addClassInfo.cat_level = this.selectedKeys.length
      }
    },
    addClass () {
      this.$refs.addClassForm.validate(async val => {
        if (val) {
          const { data: res } = await this.$http.post('categories', this.addClassInfo)
          if (res.meta.status == 201) {
            this.addClassdialogVisible = false
            this.$message.success('添加成功~')
            this.getGoodsList()
          } else {
            this.$message.error(res.meta.msg)
          }
        }
      })
    },
    closeForm () {
      this.$refs.addClassForm.resetFields()
      this.selectedKeys = []
      this.addClassInfo.cat_pid = 0
      this.addClassInfo.cat_level = 0
    }
  },
  created () {
    this.getGoodsList()
  }
}
</script>
<style scoped>
.table {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>